//
//  LoginAPI.swift
//  SwiftuiUno
//
//  Created by hccc on 2023/11/21.
//

import Foundation
import UIKit

enum SignType {
    case password_login
    case password_set
    case phone_bind
}
//0:游客登录;1:注册登录;2:密码登录;3:三方登录;4:验证码登录
enum LoginFrom: Int {
    case type_tourists_0    = 0
    case type_regiter_1     = 1
    case type_password_2    = 2
    case type_third_3       = 3
    case type_sms_code_4    = 4
}
// 注册时候必传,0，手机,1、Twitter,2、FaceBook,3、Google,4、Apple
enum LoginType: Int {
    case type_phone_0 = 0
    case type_twitter_1 = 1
    case type_facebook_2 = 2
    case type_google_3 = 3
    case type_apple_4 = 4
}

final class LoginAPI: APITargetType {
    /// 确认是否注册
    static func isRegistered(areaCode: String, phone: String) -> Self {
        .init(
            route: .get("/v1/user/isRegistered"),
            params: {
                [
                    "areaCode": areaCode,
                    "phoneNumber": phone,
                    "appType": Env.current.appType,
                    "platformType": Const.platformType,
                ]
            }
        )
    }
    /// 发送验证码
    static func smsSend(areaCode: String, phone: String, smsType: String) -> Self {
        .init(
            route: .post("/v1/user/sms/send"),
            params: {
                [
                    "areaCode": areaCode,
                    "phoneNumber": phone,
                    "sendSmsType": smsType,
                    "appType": Env.current.appType,
                    "platformType": Const.platformType,
                    "language": "en",
                ]
            }
        )
    }
    
    /// 登录
    static func login(
        loginFrom: LoginFrom,
        loginType: LoginType,
        areaCode: String? = nil,
        phoneNumber: String? = nil,
        password: String? = nil,
        smsCode: String? = nil,
        apiId: String? = nil,
        apiToken: String? = nil
    ) -> Self {
        .init(
            route: .post("/v1/user/login"),
            params: {
                [
                    "loginForm": loginFrom.rawValue,
                    "loginType": loginType.rawValue,
                    "areaCode": areaCode,
                    "phoneNumber": phoneNumber,
                    "password": CryptTool.base64Encode(password),
                    "authCode": smsCode,
                    "apiId": apiId,
                    "apiToken": apiToken,
                    // 通用
                    "appType": Env.current.appType,
                    "platformType": Const.platformType,
                    "phoneImie": AppService.phoneImei,
                    "appName": Const.displayName,
                    "appVersion": Const.appVersion,
                    "buildVersion": "40099",
                    "downloadChannel": "appStore",
                    "promotionChannel": "appStore",
                    "languageHabits": "zh_tw",
                    "platformModel": "iPhone 11 Pro",
                    "osVersion": UIDevice.current.systemVersion,
                    "ip": "0.0.0.0",
                ]
            }
        )
    }
    /// 登录
    static func quickLogin() -> Self {
        .init(
            route: .post("/v1/user/quickLogin"),
            params: {
                [
                    "loginForm": LoginFrom.type_password_2.rawValue,
                    "loginType": LoginType.type_phone_0.rawValue,
                    // 通用
                    "appType": Env.current.appType,
                    "platformType": Const.platformType,
                    "phoneImie": AppService.phoneImei,
                    "appName": Const.displayName,
                    "appVersion": Const.appVersion,
                    "buildVersion": "40099",
                    "downloadChannel": "appStore",
                    "promotionChannel": "appStore",
                    "languageHabits": "zh_tw",
                    "platformModel": "iPhone 11 Pro",
                    "osVersion": UIDevice.current.systemVersion,
                    "ip": "0.0.0.0",
                ]
            }
        )
    }
    
    /// 性别操作
    static func postUserInfo(sex: UserSex) -> Self {
        .init(
            route: .post("/v1/user/info"),
            params: {
                [
                    "sex": sex.rawValue,
                    // 通用
                    "appType": Env.current.appType,
                    "platformType": Const.platformType,
                    "phoneImie": AppService.phoneImei,
                ]
            }
        )
    }
}

